//
//  CmtShenQuListViewController.m
//  Products_B
//
//  Created by lanou on 16/6/7.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CmtShenQuListViewController.h"
#import "CmtNewCollectionCell.h"
#define kCmtShenQuUrl @"http://api.izhangchu.com/"
#import "CmtProductDetailViewController.h"
@interface CmtShenQuListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,assign)int page;

@property(nonatomic,strong)NSMutableArray *CmtNewModelArray;

@property(nonatomic,strong)UILabel *label;

@property(nonatomic,strong)UIView *topView;
@end

@implementation CmtShenQuListViewController

//自定义导航栏
-(void)setNavView{
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 70)];
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _topView.backgroundColor = [UIColor whiteColor];
    button.frame = CGRectMake(10, 30, 50, 30);
    [button setTitle:@"<—" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(buton:) forControlEvents:(UIControlEventTouchUpInside)];
    [_topView addSubview:button];
    [self.navigationController.view addSubview:_topView];
    
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake((kWidth - 100)/2, 30, 100, 30)];
    
    if (self.titleString) {
        self.label.text = [NSString stringWithFormat:@"%@",self.titleString];
    }
    else{
        self.label.text = @"精选作品";
    }
    _label.textAlignment = NSTextAlignmentCenter;
    [self.navigationController.view addSubview:_label];
    

    self.view.backgroundColor = [UIColor yellowColor];
}

-(void)buton:(UIButton *)button{

    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationController.navigationBar.translucent = NO;
    [self setNavView];
    
    UICollectionViewFlowLayout *flowLatout = [[UICollectionViewFlowLayout alloc]init];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:flowLatout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    
     [self.collectionView registerNib:[UINib nibWithNibName:@"CmtNewCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"CmtNewCell"];
    
    self.CmtNewModelArray = [[NSMutableArray alloc] initWithCapacity:10];
    [self requestCmtDataWithUrlString:nil];
    
    
    self.page = 1;
    //    上拉加载更多
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page += 1;
       
        [self requestCmtDataWithUrlString:nil];
    }];
    //  下拉刷新
    self.collectionView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self.CmtNewModelArray removeAllObjects];
        [self requestCmtDataWithUrlString:nil];
        
    }];
    

    
}

-(void)requestCmtDataWithUrlString:(NSString *)urlString{
    
    
    if (!([self.topic_id isEqualToString:@""] || self.topic_id == nil)) {
    
        NSDictionary *parDic1 = @{@"topic_id":self.topic_id,@"is_marrow":self.is_marrow,@"page":[NSString stringWithFormat:@"%d",self.page],@"methodName":@"ShequList",@"size":@"20",@"version":@"4.40&"};
        
        [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:parDic1 finish:^(NSData *data) {
            NSDictionary *jsDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            //        NSLog(@"%@",jsDic);
            
            self.CmtNewModelArray = [CmtNewModel setModelWithJSdic:jsDic];
            [self.collectionView reloadData];
            
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
            
        } error:nil];

    }
    
    else{
    
        NSDictionary *parDic = @{@"is_marrow":self.is_marrow,@"page":[NSString stringWithFormat:@"%d",self.page],@"methodName":@"ShequList",@"size":@"20",@"version":@"4.40&"};
        [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:parDic finish:^(NSData *data) {
            NSDictionary *jsDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            //        NSLog(@"%@",jsDic);
            
            self.CmtNewModelArray = [CmtNewModel setModelWithJSdic:jsDic];
            [self.collectionView reloadData];
            
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
            
        } error:nil];
    };
    
}

#pragma mark - 当push的时候隐藏tabBar（UIviewController）
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.CmtNewModelArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    CmtNewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CmtNewCell"  forIndexPath:indexPath];
    if (self.CmtNewModelArray.count > 0) {
        cell.model = self.CmtNewModelArray[indexPath.row];
    }
    
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}


-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kWidth / 2, kWidth / 2 + 70);
}

//collectview的点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CmtNewModel *model = self.CmtNewModelArray[indexPath.row];
    
    CmtProductDetailViewController *CmtSDTableVc = [[CmtProductDetailViewController alloc]init];
    CmtSDTableVc.post_id = model._id;
    [self.navigationController pushViewController:CmtSDTableVc animated:YES];
    
}

//视图将要消失-隐藏导航栏
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.parentViewController.navigationController.navigationBarHidden = YES;
    self.topView.hidden = YES;
}

//试图将要出现-出现导航栏
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.parentViewController.navigationController.navigationBarHidden = NO;
    self.topView.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
